package com.yiwei.zhh.api.requestInfo;

import com.yiwei.zhh.api.Api;
import com.yiwei.zhh.api.bean.CodeBean;
import com.yiwei.zhh.api.bean.LoginBean;
import com.yiwei.zhh.api.bean.ModifyPhoneBean;
import com.yiwei.zhh.api.bean.ModifyPhoneCheckCodeBean;
import com.yiwei.zhh.api.bean.ModifyPhoneSendCodeBean;
import com.yiwei.zhh.api.callback.ChangePhoneContract;
import com.yiwei.zhh.api.callback.LoginContract;
import com.yiwei.zhh.util.Network;
import com.yiwei.zhh.util.RxLifeUtil;
import com.yiwei.zhh.util.ToastUtil;

/**
 * Created by Administrator on 2017/5/14.
 */

public class ChangePhoneRequest extends BaseRequest<ChangePhoneContract.View> implements ChangePhoneContract.Presenter<ChangePhoneContract.View> {

    public static ChangePhoneRequest create() {
        return new ChangePhoneRequest();
    }


    @Override
    public void getModifyphoneSendcode(String mobile) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        ModifyPhoneSendCodeBean bean = new ModifyPhoneSendCodeBean();
        bean.mobile = mobile;
        Api.api_service.getModifyphoneSendcode(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getModifyphoneSendcode(result);
                });
    }

    @Override
    public void getModifyphoneCheckcode(String mobile, String verify_code) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        ModifyPhoneCheckCodeBean bean = new ModifyPhoneCheckCodeBean();
        bean.mobile = mobile;
        bean.verify_code = verify_code;
        Api.api_service.getModifyphoneCheckcode(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getModifyphoneCheckcode(result);
                });
    }

    @Override
    public void getModifyphone(String mobile, String verify_code, String token) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        ModifyPhoneBean bean = new ModifyPhoneBean();
        bean.mobile = mobile;
        bean.token = token;
        bean.verify_code = verify_code;
        Api.api_service.getModifyphone(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getModifyphone(result);
                });
    }

    @Override
    public void getCode(String type, String mobile) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        CodeBean bean = new CodeBean();
        bean.mobile = mobile;
        bean.type = type;
        Api.api_service.getCode(bean).compose(RxLifeUtil.NocheckOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();

                    } catch (NullPointerException e) {

                    }

                }).
                subscribe(result -> {
                    if (result.getStatus() ==1) {
                        mView.getCodeSuccess(result);
                    } else {
                        ToastUtil.makeText(BaseContext, result.getMsg()).show();
                    }
                });
    }
}
